#include "AcceptConnectionThread.h"
#include "Server.h"
#include "Networking.h"
#include "Engine.h"
#include "GameState.h"

void AcceptConnectionThread::Work()
{
    Networking::Socket sock;
    if(!sock.OpenServer(m_listenPort))
    {
        OUT_STRING("Could not open Server");
        return;
    }

    OUT_STRING("Server Opened, listening to port: " << m_listenPort );
    while(m_accepting)
    {
        Networking::Socket* connection = new Networking::Socket;
        while(!sock.AcceptConnection(connection))
        {
            if(m_accepting)
                SDL_Delay(1000);
            else
                break;
        }
        TheServer::Instance()->AddClient(connection);
    }
}
